<?php
/**
 * H5支付.
 */

namespace app\api\service\payment;

use app\api\service\ApiPayment;
use app\common\library\exception\OrderException;
use app\api\service\payment\Unionpay\Request;
use function GuzzleHttp\json_decode;

class Unionpay extends ApiPayment
{
    public function alipay($order)
    {
        return $this->requestApi($order);
    }

    public function weixin($order)
    {
        return $this->requestApi($order, 'weixin');
    }
    public function jdpay($order)
    {
        return $this->requestApi($order, 'jdpay');
    }

    public function unionpay($order)
    {
        return $this->requestApi($order, 'unionpay');
    }

    public function requestApi($order, $type = 'alipay')
    {
        $params = [
            'out_trade_no' => $order['trade_no'],
            'body' => $order['subject'],
            'attach' => $order["extra"],
            'total_fee' => bcmul($order['amount'], 100),
            'mch_create_ip' => $_SERVER['REMOTE_ADDR'],
            'time_start' => '', //订单生成时间
            'time_expire' => '', //订单超时时间
        ];

        $req = new Request($this->config);

        //weixin,alipay, jdpay, unionpay
        $result = $req->submitOrderInfo($type, $params, "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}/notify/Unionpay");
        if ($result['code_img_url']) {
            return [
                'order_id' => $order['out_trade_no'],
                'pay_seq' => $order['trade_no'],
                'pay_url' => $result['code_img_url'],
                'pay_app_url' => $result['code_url']
            ];
        }
    }

    public function notify()
    {
        return $this->verifyOrderNotify();
    }

    /**
     * 回调验签.
     *
     *
     * @return array
     *
     * @throws OrderException
     */
    private function verifyOrderNotify()
    {
//         $xml = <<<XML
// <xml><attach><![CDATA[扩展字段]]></attach>
// <bank_type><![CDATA[ALIPAYACCOUNT]]></bank_type>
// <charset><![CDATA[UTF-8]]></charset>
// <fee_type><![CDATA[CNY]]></fee_type>
// <invoice_amount><![CDATA[100]]></invoice_amount>
// <mch_id><![CDATA[7551000001]]></mch_id>
// <nonce_str><![CDATA[1560933015499]]></nonce_str>
// <openid><![CDATA[2088002003181694]]></openid>
// <out_trade_no><![CDATA[190619162925867]]></out_trade_no>
// <out_transaction_id><![CDATA[2019061922001481690595140213]]></out_transaction_id>
// <pay_result><![CDATA[0]]></pay_result>
// <result_code><![CDATA[0]]></result_code>
// <sign><![CDATA[5B6C536DC5230B9B4D0F50EEF13C64B3]]></sign>
// <sign_type><![CDATA[MD5]]></sign_type>
// <status><![CDATA[0]]></status>
// <time_end><![CDATA[20190619163014]]></time_end>
// <total_fee><![CDATA[100]]></total_fee>
// <trade_type><![CDATA[pay.alipay.native]]></trade_type>
// <transaction_id><![CDATA[7551000001201906193299399214]]></transaction_id>
// <version><![CDATA[2.0]]></version>
// </xml>
// XML;
        $xml = file_get_contents('php://input');
        libxml_disable_entity_loader(true);
        //Object  对象
        $response = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
        //读订单对应的支付渠道配置
        $this->config = self::getOrderPayConfig($response['out_trade_no']);
        $req = new Request($this->config);
        $result = $req->callback();
        if ($result) {
            return array(
                'trade_no' => $result['out_trade_no'],
            );
        }
        
        return false;
    }

}
